package com.dragonsoftbravo.sqlbuilder.sql.where.builder;

import com.dragonsoftbravo.sqlbuilder.SqlContext;
import com.dragonsoftbravo.sqlbuilder.sql.Builder;

/**
 * Created by DI on 2016/5/15.
 */
public class Comp extends Builder {
    public Comp(StringBuilder sqlBuilder) {
        super(sqlBuilder);
    }

    private static Comp comp(String op, String arg1, String arg2){
        return new Comp(new StringBuilder().append(arg1).append(" ").append(op).append(" ").append(arg2));
    }

    public static Comp equal(String colum1, String colum2){
        return comp("=", SqlContext.getColumn(colum1),SqlContext.getSqlValue(colum2));
    }

    public static Comp equal(String colum1, Object val){
        return comp("=",SqlContext.getColumn(colum1),SqlContext.getSqlValue(val));
    }

    public static Comp notEqual(String colum1, String colum2){
        return comp("<>",SqlContext.getColumn(colum1),SqlContext.getColumn(colum2));
    }

    public static Comp notEqual(String colum1, Object val){
        return comp("<>",SqlContext.getColumn(colum1),SqlContext.getSqlValue(val));
    }

    public static Comp lt(String colum1, String colum2){
        return comp("<",SqlContext.getColumn(colum1),SqlContext.getColumn(colum2));
    }

    public static Comp lt(String colum1, Object val){
        return comp("<",SqlContext.getColumn(colum1),SqlContext.getSqlValue(val));
    }

    public static Comp gt(String colum1, String colum2){
        return comp(">",SqlContext.getColumn(colum1),SqlContext.getColumn(colum2));
    }

    public static Comp gt(String colum1, Object val){
        return comp(">",SqlContext.getColumn(colum1),SqlContext.getSqlValue(val));
    }

    public static Comp lte(String colum1, String colum2){
        return comp("<=",SqlContext.getColumn(colum1),SqlContext.getColumn(colum2));
    }

    public static Comp lte(String colum1, Object val){
        return comp("<=",SqlContext.getColumn(colum1),SqlContext.getSqlValue(val));
    }

    public static Comp gte(String colum1, String colum2){
        return comp(">=",SqlContext.getColumn(colum1),SqlContext.getColumn(colum2));
    }

    public static Comp gte(String colum1, Object val){
        return comp(">=",SqlContext.getColumn(colum1),SqlContext.getSqlValue(val));
    }

    public static Comp between(String colum, Object val1, Object val2){
        return comp("BETWEEN",SqlContext.getColumn(colum),SqlContext.getSqlValue(val1) +" AND "+SqlContext.getSqlValue(val2));
    }

    public static Comp in(String colum, Object ...ins){
        StringBuilder sbIns = new StringBuilder();
        sbIns.append("(");
        for(Object val:ins){
            sbIns.append(SqlContext.getSqlValue(val)).append(",");
        }
        sbIns.deleteCharAt(sbIns.length()-1);
        sbIns.append(")");
        return comp("IN",SqlContext.getColumn(colum),sbIns.toString());
    }

    public static Comp like(String colum, String like){
        return comp("LIKE",SqlContext.getColumn(colum),SqlContext.getSqlValue(like));
    }
}
